/*
* @(#)ListUserDelegationAction.java
*
* Copyright (c) 2003 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
*/
package com.dcivision.user.web;

import java.sql.Connection;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionMapping;

import com.dcivision.alert.bean.MtmUpdateAlertRecipient;
import com.dcivision.alert.bean.UpdateAlert;
import com.dcivision.alert.dao.MtmUpdateAlertRecipientDAObject;
import com.dcivision.alert.dao.UpdateAlertDAObject;
import com.dcivision.alert.dao.UpdateAlertSystemLogDAObject;
import com.dcivision.alert.dao.UpdateAlertTypeDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.dao.AbstractDAObject;
import com.dcivision.framework.web.AbstractListAction;
import com.dcivision.framework.web.AbstractSearchForm;
import com.dcivision.user.bean.UserDelegation;
import com.dcivision.user.dao.UserDelegationDAObject;

/**
 * <p>Class Name:       ListUserDelegationAction.java    </p>
 * <p>Description:      The list action class for ListUserGroup.jsp</p>
 *
 *    @author           Angus Shiu
 *    @company          DCIVision Limited
 *    @creation date    24/03/2003
 *    @version          $Revision: 1.8.2.1 $
 */

public class ListUserDelegationAction extends AbstractListAction {

  public static final String REVISION = "$Revision: 1.8.2.1 $";


  /**
   *  Constructor - Creates a new instance of ListUserDelegationAction and define the default listName.
   */
  public ListUserDelegationAction() {
    super();
    this.setListName("userDelegationList");
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  public String getMajorDAOClassName() {
    return("com.dcivision.user.dao.UserDelegationDAObject");
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode() {
    return (SystemFunctionConstant.WORKFLOW_DELEGATION) ;
  }

  /**
   * getListData
   *
   * Override the parent's function. Since it required to get the user role
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @throws ApplicationException
   */
  public void getListData(ActionMapping mapping, AbstractSearchForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    super.getListData(mapping, form, request, response);
/*
    // Get the user roles of each user record.
    List listResult = (ArrayList)request.getAttribute(this.getListName());
    List listRole = new ArrayList();
    UserRoleDAObject userRoleDAO = new UserRoleDAObject(this.getSessionContainer(request), this.getConnection(request));
    for (int i=0; i<listResult.size(); i++) {
      UserGroup userGroup = (UserGroup)listResult.get(i);
      listRole.add(userRoleDAO.getListByUserGroupID(userGroup.getID()));
    }
    request.setAttribute(VAR_USER_ROLE_LIST, listRole);
*/
  }
  //king---add---start
  /**
   *  Method deleteListData() - delete the item(s) from listing page
   *
   *  @param      mapping               ActionMapping from struts
   *  @param      form                  ActionForm from struts
   *  @param      request               HttpServletReuqest
   *  @param      respond               HttpServletRespond
   *  @return     void
   *  @throws     ApplicationException  Throws ApplicationException the deleteion failed
   */
  public void deleteListData(ActionMapping mapping, AbstractSearchForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    AbstractDAObject dao = this.getMajorDAObject(request);
    if("USER_DELEGATION".equals(dao.getTableName())){
      this.delNotifyTargetActor(form, request);
    }
    dao.deleteList(form.getBasicSelectedID());
  }
  
  public void delNotifyTargetActor(AbstractSearchForm form, HttpServletRequest request) throws ApplicationException {
    SessionContainer sessionCon = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    UserDelegationDAObject delegationDAO=new UserDelegationDAObject(sessionCon,conn);
    UpdateAlertDAObject updageAlertDAO=new UpdateAlertDAObject(sessionCon,conn);
    UpdateAlertTypeDAObject updateAlertTypeDAO=new UpdateAlertTypeDAObject(sessionCon,conn);
    UpdateAlertSystemLogDAObject updateAlertSystemLogDAO=new UpdateAlertSystemLogDAObject(sessionCon,conn);
    MtmUpdateAlertRecipientDAObject mtmUpdateAlertRecipientDAO=new MtmUpdateAlertRecipientDAObject(sessionCon,conn);
    String[] delegationIDs=form.getBasicSelectedID();
    for(int i=0;i<delegationIDs.length;i++){
      Integer delegationID=new Integer(delegationIDs[i]);     
      UserDelegation tmpUserDelegation=(UserDelegation)delegationDAO.getObjectByID(delegationID);
      List updateAlertList=updageAlertDAO.getByObjectTypeObjectIDList(UpdateAlert.DELEGATION_TYPE,tmpUserDelegation.getID());
      for(int j=0;j<updateAlertList.size();j++){
        UpdateAlert tmpUpdateAlert=(UpdateAlert)updateAlertList.get(j);   
        List mtmUpdateAlertRecipientList=mtmUpdateAlertRecipientDAO.getByUpdateAlertID(tmpUpdateAlert.getID());
        for(int k=0;k<mtmUpdateAlertRecipientList.size();k++){
          MtmUpdateAlertRecipient tmpMtmUpdateAlertRecipient=(MtmUpdateAlertRecipient)mtmUpdateAlertRecipientList.get(k);
          //mtmUpdateAlertRecipientDAO.deleteByID(tmpMtmUpdateAlertRecipient.getID());
        }        
        //UpdateAlertType tmpUpdateAlertType=(UpdateAlertType)updateAlertTypeDAO.getByupdateAlertID(tmpUpdateAlert.getID());      
        
       // updateAlertSystemLogDAO.deleteByUpdateAlertTypeID(tmpUpdateAlertType);        
        updateAlertTypeDAO.deleteByUpdateAlertID(tmpUpdateAlert.getID(),null);        
        updageAlertDAO.deleteByID(tmpUpdateAlert.getID());
      }
    }
  }
  //king---add---end
}
